package com.hdoit.ares.web.official.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.hdoit.ares.logical.official.service.IArticalService;
import com.hdoit.ares.logical.viewmodel.official.ArticalSearch;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月10日 09:56:30
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/official/artical/")
public class ArticalController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/official/artical/";
    }

    private IArticalService articalService;
    private IArticalClassifyService articalClassifyService;
    
    public IArticalService getArticalService() {
        return articalService;
    }
    @Resource
    public void setArticalService(IArticalService articalService) {
        this.articalService = articalService;
    }
    
    public IArticalClassifyService getArticalClassifyService() {
		return articalClassifyService;
	}
    @Resource
	public void setArticalClassifyService(IArticalClassifyService articalClassifyService) {
		this.articalClassifyService = articalClassifyService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="articalDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    
    private final String realUrlViewName = "http://www.putiyoli.net/ares/admin/share/share/articalDetail?id=";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ArticalSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(articalService.queryCount(search));
        List<Artical> articals= articalService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("articals", articals);
        model.addAttribute("articalSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String articalDetailed(Model model,String key){
        Artical artical=new Artical();
        artical.setDisplay(YesOrNoInteger.yes);
        if(!StringUtils.isEmpty(key)){
            artical=articalService.queryModel(key);
        }
        model.addAttribute("artical", artical);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Artical artical){
        if(StringUtils.isEmpty(artical.getId())){
            artical.setId(CommonUtils.getKeyId());
            artical.setCreateTime(DateUtils.getCurrent());
            artical.setDeleteMark(YesOrNo.no);
            artical.setModelStatus(ModelStatus.add);
            artical.setFileUrl(realUrlViewName + artical.getId());
        }else{
            artical.setModelStatus(ModelStatus.update);
        }
        ArticalClassify articalClassify = articalClassifyService.queryModel(artical.getTypeId());
        artical.setStyle(articalClassify.getName());
        ActionResult actionResult;
        if(articalService.applyChange(artical)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("artical", artical);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
    	
    	
        if(articalService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
   
}